﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Web.SystemConfig.Services;
using Microsoft.Practices.Unity;
using System.Data;
using Common;
using CommonModule;
using Web.Views.Public;

namespace Web.Views.SystemConfig
{
    public partial class UpdateUserPasswd : PageBase
    {

        #region 变量定义
        /// <summary>
        /// Business类
        /// </summary>
        [Dependency]
        public IUpdateUserPasswd BUSUpdateUserPasswd
        {
            get;
            set;
        }
        #endregion


        protected System.Web.UI.WebControls.Literal litIsinDep;

        #region Initial Status bar
        private void InitStatus()
        {
            //this.TabControl1.Items[0].Caption = GetPublicString("string", "BasicInfomation");
        }
        #endregion

        private void InitControlsCultureInfo()
        {
            this.TabControl1.Items[0].Caption = GetPublicString("string", "updatepassword");
            this.litChangePassword.Text = GetString("litChangePassword");
            this.litDepartment.Text = GetString("litDepartment");
            this.litUserID.Text = GetString("litUserID");
            this.litPassword.Text = GetString("litPassword");
            this.RequiredFieldPassword.ErrorMessage = GetString("sRequiredField1");
            this.litPassword2.Text = GetString("litPassword2");
            this.RequiredFieldPass2.ErrorMessage = GetString("sRequiredField2");
            this.btnChangePassword.Text = GetString("btnChangePassword");
        }
        private void BindDepartment()
        { 
            DataTable dtOrg = BUSUpdateUserPasswd.SelectStructedDepartments();
            this.ddlDepartmentList.DataSource = dtOrg;
            this.ddlDepartmentList.DataTextField = "ORG_Name";
            this.ddlDepartmentList.DataValueField = "ORG_ID";
            this.ddlDepartmentList.DataBind();

            this.ddlDepartmentList.Items.Add(new ListItem(GetString("liOtherDep"), "<other>"));
            this.ddlDepartmentList.Items.Insert(0, new ListItem(GetString("liAllDep"), "<all>"));

            this.BindUsers() ;

        }

        //权限检查，待处理
        //private void CheckAuthorities()
        //{
        //    if (!base.HasAuthority("ViewSingle"))
        //    {
        //        base.RedirectToDefaultErrorPage("NotAuthorized", "Need <b>SetPasswd</b> Authority for this module");
        //    }
        //}
        protected void Page_Load(object sender, System.EventArgs e)
        {
            SetPageProperty();
            CreateToolMenu(ToolBar1, ToolMenuType.TYPE_Edit);
            InitControlsCultureInfo();

            if (!Page.IsPostBack)
            {
                //权限检查，待处理
                //this.CheckAuthorities();
                BindDepartment();
                InitStatus();
            }
            base.CheckEditAuthorities(CurrentPageState);
        }

        /// <summary>
        /// 设置页面的菜单属性
        /// </summary>
        private void SetPageProperty()
        {
            this.DBHelpName = DBHelperString.Dictionary;
            base.MenuID = "ePlanet.Configuration.UpdateUserPasswd";
            if (!IsPostBack)
            {
                base.CurrentPageState = PageState.State_Edit;
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            SetToolBarVisibile();
            base.OnPreRender(e);
        }

        private void SetToolBarVisibile()
        {
            ToolBarHelper.SetToolBarVisible(ToolBar1, ToolBarID.Add, false);
            ToolBarHelper.SetToolBarVisible(ToolBar1, ToolBarID.Delete, false);
            ToolBarHelper.SetToolBarVisible(ToolBar1, ToolBarID.Export, false);
            //ToolBarHelper.SetToolBarVisible(ToolBar1, ToolBarID.Help, false);
            ToolBarHelper.SetToolBarVisible(ToolBar1, ToolBarID.Save, false);
            ToolBarHelper.SetToolBarVisible(ToolBar1, ToolBarID.Cancel, false);
        }

        #region Web Form Designer generated code
        /// <summary>
        /// 
        /// </summary>
        /// <param name="e"></param>
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {

        }
        #endregion

        protected void btnChangePassword_Click(object sender, System.EventArgs e)
        {
            try
            {
                string userID = this.ddlUserList.SelectedValue;
                string pass = EncryptDecrypt.EncryptPassword(this.txtPassword.Text.Trim(), "MD5");
                if (BUSUpdateUserPasswd.UpdateUserPasswd(userID, pass) == 1)
                {
                    base.Alert(GetString("sUpdatePasswordSuc"));

                }
                else
                {
                    base.Alert(GetString("sUpdatePasswordFaild"));
                };
            }
            catch
            {
                base.Alert(GetString("UpdatePasswordError"));
            }
        }

        private void BindUsers()
        {
            DataTable dtUser = new DataTable();
            if (this.ddlDepartmentList.SelectedIndex > 0)
            {
                string depID = this.ddlDepartmentList.SelectedValue;
                if (depID == "<other>")
                {
                    depID = null;
                }
                dtUser = BUSUpdateUserPasswd.SelectUserInfoByDepID(depID);
            }
            else
            {
                dtUser = BUSUpdateUserPasswd.SelectUserInfoByDepID(null);
            }

            this.ddlUserList.DataSource = dtUser;
            this.ddlUserList.DataTextField = "FullName";
            this.ddlUserList.DataValueField = "User_ID";
            this.ddlUserList.DataBind();

            if (this.ddlUserList.Items.Count == 0)
            {
                this.btnChangePassword.Enabled = false;
            }
            else
            {
                this.btnChangePassword.Enabled = true;
            }
        }
        protected void ddlDepartmentList_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            this.BindUsers();
        }
    }
}
